//
//  ZSY_UserSettingViewController.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 16/1/9.
//  Copyright © 2016年 张诗宇. All rights reserved.
//

#import "ZSY_UserSettingViewController.h"
#import "ZSY_UserSettingCell.h"
#import "ZSY_MyCollectionViewController.h"
#import "ZSY_RecommendViewController.h"

@interface ZSY_UserSettingViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) ZSY_BaseTableView *myTableView;
@property (nonatomic, retain) UISwitch *switchView;
@property (nonatomic, retain) ZSY_UserSettingCell *usersCell;


@end

@implementation ZSY_UserSettingViewController

- (void)dealloc {
    [_switchView release];
    [_myTableView release];
    [_usersCell release];
    [super dealloc];
}

#pragma 开关UISwitch相关
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"];
    if ([str isEqualToString:@"yes"]) {
        self.switchView.on = YES;
    } else {
        self.switchView.on = NO;
    }
}

- (void)setName:(NSString *)name {
    if (_name != name) {
        [_name release];
        _name = [name retain];
        self.usersCell.name = _name;
    }
}

- (void)setIconUrl:(NSString *)iconUrl {
    if (_iconUrl != iconUrl) {
        [_iconUrl release];
        _iconUrl = [iconUrl retain];
        self.usersCell.iconUrl = _iconUrl;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"设置";

#pragma 开关UISwitch相关
    self.switchView = [[UISwitch alloc] init];
    
    [self.switchView addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"account_uploadavatar_btn"] forBarMetrics:UIBarMetricsDefault];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_new_poi_back_btn"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    CGRect frame = self.view.frame;
    frame.size.height -= 84;
    self.myTableView = [[ZSY_BaseTableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.view addSubview:_myTableView];
    
    [self.myTableView registerClass:[ZSY_UserSettingCell class] forCellReuseIdentifier:@"userCell"];
    
    [_myTableView release];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 7;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return 3;
    } else if (section == 4) {
        return 2;
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";
    
    ZSY_BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[ZSY_BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    
    if (indexPath.section == 0) {
        self.usersCell = [tableView dequeueReusableCellWithIdentifier:@"userCell"];
        self.usersCell.name = _name;
        self.usersCell.iconUrl = _iconUrl;
        return _usersCell;
    } else if (indexPath.section == 1) {
        cell.textLabel.text = @"我的收藏";
        cell.detailTextLabel.text = @" 〉";
        
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"修改账户密码";
            cell.detailTextLabel.text = @" 〉";
            
        } else if (indexPath.row == 1) {
            cell.textLabel.text = @"推送通知设置";
            cell.detailTextLabel.text = @" 〉";
            
        } else if (indexPath.row == 2) {
            cell.textLabel.text = @"链接社交网络";
            cell.detailTextLabel.text = @" 〉";
        }
    } else if (indexPath.section == 3) {
        cell.textLabel.text = @"清楚缓存";
//        NSInteger size = [[SDImageCache sharedImageCache]getSize];
//        float sizeMB = size/1024.0/1024.0;
        cell.detailTextLabel.text = @" 〉";
        
    } else if (indexPath.section == 4) {
        if (indexPath.row ==0) {
            cell.textLabel.text = @"关于我们";
            cell.detailTextLabel.text = @" 〉";
        }
        else if (indexPath.row == 1) {
            cell.textLabel.text = @"意见反馈";
            cell.detailTextLabel.text = @" 〉";
            
        }
//        else if (indexPath.row == 1) {
////            cell.textLabel.text = @"喜欢「 MOVE 」? 给个5✨好评吧 :-)";
////            cell.detailTextLabel.text = @" 〉";
//        }
    } else if (indexPath.section == 6) {
        cell.textLabel.text = @"精品旅行工具应用";
        cell.detailTextLabel.text = @" 〉";
    } else {
        self.switchView.tag = indexPath.section;
        cell.textLabel.text = @"夜间模式";
#pragma 开关UISwitch相关
        cell.accessoryView = self.switchView;
    }
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 80;
    } else {
        return 36;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (void)leftButtonAction:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma 开关UISwitch相关
- (void)switchAction {
    NSString *states = [[NSUserDefaults standardUserDefaults] objectForKey:@"night"]
    ;
    if ([states isEqualToString:@"yes"]) {
        // 夜间模式
        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"night"];
        // 发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:WHITE_COLOR];
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"night"];
        // 发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:NIGHT_COLOR];
    }
    
    
    
}

// cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //点击后自动取消选中置灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    cell.selected = NO;
    
    if (indexPath.section == 1) {
        ZSY_MyCollectionViewController *vc = [[ZSY_MyCollectionViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
    
    if (indexPath.section == 3) {
        NSInteger size = [[SDImageCache sharedImageCache]getSize];
        float sizeMB = size/1024.0/1024.0;
        NSLog(@"清除前%.2fMB",sizeMB);
        
        UIAlertController *alart = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"确定清除%.1fMB缓存", sizeMB] preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [[SDImageCache sharedImageCache] clearDisk];
            // [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%.2fMB",sizeMB] forKey:@"caches"];
            NSLog(@"清除后%.2fMB",sizeMB);
//            [self.myTableView reloadData];
            
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alart addAction:sure];
        [alart addAction:cancel];
        [self presentViewController:alart animated:YES completion:^{
            
        }];
        
    }
    if (indexPath.section == 6) {
        ZSY_RecommendViewController *vc = [[ZSY_RecommendViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
